//
//  YD3rdUserInfoConfirmViewController.m
//  IngCard
//
//  Created by kf on 2017/8/28.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YD3rdInfoConfirmViewController.h"
#import "YD3rdInfoProfileTableViewCell.h"
#import "YDAccountManager.h"
#import "YDAuthManager.h"
#import "LinkedInHelper.h"
#import "YD3rdInfoSectionTableViewCell.h"
#import "YDAccountManager.h"
#import "LinkedInHelper.h"
#import "YDLinkedInAccount.h"
#import "YDUserExperienceTableViewCell.h"

@interface YD3rdInfoConfirmViewController ()
@property(nonatomic, strong) UIButton *confirmButton;
@end

@implementation YD3rdInfoConfirmViewController

-(void) viewDidLoad
{
    [super viewDidLoad];
    self.title = STRING.user_3rd_confirmInfo;
}
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [THEME renderStatusBarStyle:YDStatusBarStyle_Light];
    [THEME renderNavigationBar:self style:YDNavigationStyle_Green];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
    [THEME renderStatusBarStyle:YDStatusBarStyle_Light];
}

-(void) initDataSource
{
    [super initDataSource];
    
    [AUTH get3rdAccountDetail:YD3rdSignID_LinkedIn
            accessToken:[LinkedInHelper sharedInstance].accessToken
                    uid:ACCOUNT.account.uid
             loginToken:ACCOUNT.account.login_token
             onComplete:^(BOOL success, NSString *msg)
     {
        if(success)
        {
            [self.dataSource addDataSource:[YD3rdInfoProfileCellDataSource class]
                                 atSection:0
                                    config:^(YD3rdInfoProfileCellDataSource * dataSource)
             {
                 dataSource.name = AUTH.linkedInAccount.name;
                 dataSource.title = AUTH.linkedInAccount.occupation;// @"xxxxx";
                 dataSource.job = AUTH.linkedInAccount.companyName;// @"wsssss";
                 dataSource.location = AUTH.linkedInAccount.location;// @"sxxw234234";
                 dataSource.email = AUTH.linkedInAccount.email;
             }];

            [self.dataSource addDataSource:[YD3rdInfoSectionCellDataSource class]
                                 atSection:1
                                    config:^(YD3rdInfoSectionCellDataSource* dataSource)
            {
                dataSource.titleImage = UIImageMake(@"user_3rd_info_job");
                dataSource.title = STRING.user_3rd_info_job;
            }];
            
            for (YDLinkedInAccountPosition *position in ACCOUNT.linkedInAccount.positions) {
                
                [self.dataSource addDataSource:[YDUserExperienceCellDataSource class]
                                     atSection:1 config:^(YDUserExperienceCellDataSource* dataSource)
                {
                    dataSource.title = position.title;
                    dataSource.detail = position.company;
                    dataSource.content = NSStringFormat(@"%@年%@月",position.startYear,position.startMonth);
                }];
            }

            [self.dataSource addDataSource:[YD3rdInfoSectionCellDataSource class]
                                 atSection:2
                                    config:^(YD3rdInfoSectionCellDataSource* dataSource)
             {
                 dataSource.titleImage = UIImageMake(@"user_3rd_info_web");
                 dataSource.title = STRING.user_3rd_info_url;
                 dataSource.content = AUTH.linkedInAccount.profileUrl;
             }];
        }
    }];
}

- (void)confirmButtonAction:(UIButton *)btn
{
    [AUTH authBindingAccountDetail:YD3rdSignID_LinkedIn
                    accessToken:[LinkedInHelper sharedInstance].accessToken
                            uid:ACCOUNT.account.uid
                     loginToken:ACCOUNT.account.login_token
                     onComplete:^(BOOL success, NSString *msg)
    {
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 0;
    }
    else
    {
        return 19;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section != 0)
    {
        UIView* view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 19)];
        view.backgroundColor = UIColorMake(248, 248, 248);
        return view ;
    }
    else
    {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2)
    {
        return 190;
    }
    else
    {
        return 0;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 2)
    {
        UIView* view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 19)];
        view.backgroundColor = UIColorClear;
        
        [view addSubview:self.confirmButton];
        [self.confirmButton makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view).offset(80);
            make.centerX.equalTo(view);
            make.width.equalTo(251);
            make.height.equalTo(44);
        }];
        return view ;
    }
    else
    {
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - lazy loading
- (UIButton *) confirmButton
{
    if (!_confirmButton)
    {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmButton.frame = CGRectMake(0, 0, 251, 44);
        _confirmButton.layer.cornerRadius = 6;
        _confirmButton.layer.masksToBounds = YES;
        
        [_confirmButton setStyle:^(UIView *target) {
            UIBUTTONSTYLE;
            [temp setTitle:STRING.confirm forState:UIControlStateNormal];
            [temp setBackgroundImage:[UIImage qmui_imageWithColor:UIColorMake(102, 186, 141)] forState:UIControlStateNormal];
            [temp setBackgroundImage:[UIImage qmui_imageWithColor:UIColorMake(187, 187, 187)] forState:UIControlStateDisabled];
            temp.titleLabel.font = UIMediumFontMake(17);
        }];
        
        [_confirmButton addTarget:self
                           action:@selector(confirmButtonAction:)
                 forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}

@end
